package cn.gson.fengboot.controller;

import cn.gson.fengboot.aspect.MyLog;
import cn.gson.fengboot.config.AppConfig;
import cn.gson.fengboot.lib.MESSAGEXsend;
import cn.gson.fengboot.lib.MessageTemplate;
import cn.gson.fengboot.model.pojos.Dcare;
import cn.gson.fengboot.model.pojos.Dsetcare;
import cn.gson.fengboot.model.pojos.TfylinkmanForm;
import cn.gson.fengboot.model.service.DcareService;
import cn.gson.fengboot.utils.ConfigLoader;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/care")
public class DcareController {

    @Autowired
    DcareService service;



    /*查询生日设置*/
    @RequestMapping("/selectsetca")
    public Dsetcare selectsetce(){
        return service.selectsetce();
    };

    /*修改生日设置*/
    @RequestMapping("/editsetca")
    @MyLog(modul = "售前模块",desc = "修改生日设置")
    public void editsetca(@RequestBody Dsetcare dsetcare){
        String context = dsetcare.getSetcaContext();
        editmestem(context);
        service.editsetca(dsetcare);
    };


    /*修改生日模板*/
    public void editmestem(String context){
        AppConfig config = ConfigLoader.load(ConfigLoader.ConfigType.Message);
        MessageTemplate submail = new MessageTemplate(config);

        submail.putTemplateId("9dBPu3");
        submail.putSmsTitle("兴盛科技");
        submail.putSmsSignature("【兴盛科技】");
        submail.putSmsContent(context);
        submail.putTemplate();

    }



    /*查询节日设置*/
    @RequestMapping("/selectfestivalsetce")
    public Dsetcare selectfestivalsetce(){
        return service.selectfestivalsetce();
    };


    /*修改节日设置*/
    @RequestMapping("/editfestivalsetcare")
    @MyLog(modul = "售前模块",desc = "修改节日设置")
    public void editfestivalsetcare(@RequestBody Dsetcare dsetcare){
        String context = dsetcare.getSetcaContext();
        editeditfestivaltem(context);
        service.editfestivalsetcare(dsetcare);
    };


    /*修改节日模板*/
    public void editeditfestivaltem(String context){
        AppConfig config = ConfigLoader.load(ConfigLoader.ConfigType.Message);
        MessageTemplate submail = new MessageTemplate(config);

        submail.putTemplateId("CDOah3");
        submail.putSmsTitle("兴盛科技");
        submail.putSmsSignature("【兴盛科技】");
        submail.putSmsContent(context);
        submail.putTemplate();

    }



    /*查询生日祝福列表*/
    @RequestMapping("/selectBirth")
    public Map<String, Object> selectlinkman(HttpServletRequest request, String birarr, Integer currentpage,
                                             Integer pagesize) {
        Page<Object> obj = PageHelper.startPage(currentpage, pagesize);
        System.out.println(service.selectlinkman(request,birarr));
        Map<String, Object> map = new HashMap<>();
        map.put("total", obj.getTotal());
        map.put("rows", obj.getResult());
        return map;
    }

    /*发送生日祝福短信方法*/
    public void lastste(String modeil,String name,String sex){
        AppConfig config = ConfigLoader.load(ConfigLoader.ConfigType.Message);
        MESSAGEXsend submail = new MESSAGEXsend(config);
        submail.addTo(modeil);
        submail.setProject("9dBPu3");
        submail.addVar("name", name);
        submail.addVar("sex", sex);
        String response=submail.xsend();
        System.out.println("接口返回数据："+response);
    }

    /*发送节日祝福短信方法*/
    public void lastferste(String modeil,String name,String sex){
        AppConfig config = ConfigLoader.load(ConfigLoader.ConfigType.Message);
        MESSAGEXsend submail = new MESSAGEXsend(config);
        submail.addTo(modeil);
        submail.setProject("CDOah3");
        submail.addVar("name", name);
        submail.addVar("sex", sex);
        String response=submail.xsend();
        System.out.println("接口返回数据："+response);
    }



    /*查询发送生日祝福历史*/
    @RequestMapping("/selectcare")
    public Map<String, Object> selectcare(String setarr,Integer currentpage,
                                  Integer pagesize){
        Page<Object> obj = PageHelper.startPage(currentpage, pagesize);
        service.selectcare(setarr);
        Map<String, Object> map = new HashMap<>();
        map.put("total", obj.getTotal());
        map.put("rows", obj.getResult());
        return map;
    }

}
